package com.hns.phone.pushMsg.component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hns.can.base.component.BaseQuartzJob;
import com.hns.frame.util.SpringContextUtil;
import com.hns.phone.entity.SysAppCanPhoneRelEntity;
import com.hns.phone.pushMsg.enumrate.MsgKeys;
import com.hns.phone.pushMsg.service.PushAlarmService;
import com.hns.phone.pushMsg.util.AndListNotification;
import com.hns.phone.pushMsg.util.AndroidNotification;
import com.hns.phone.pushMsg.util.IOSListNotification;
import com.hns.phone.pushMsg.util.UmengNotification;
import com.hns.phone.pushMsg.vo.PushMessageParamsVo;
import com.hns.phone.pushMsg.vo.PushMsgDataVo;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 报警推送调度任务
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Component
public class PushAlarmQuartzJob implements BaseQuartzJob {
	
	protected static final Logger log = LoggerFactory.getLogger(PushAlarmQuartzJob.class);
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	@Autowired
	private PushAlarmService pushAlarmService;
	
	/**
	 * 推送报警，并且
	 * @throws Exception
	 * @see com.hns.can.base.component.BaseQuartzJob#runQuartzJob()
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@Override
	public void runQuartzJob() throws Exception {
		System.out.println("~~~~~~~~~~~~~~~runQuartzJob");
		//更新库表
		pushAlarmService.updateDayReport();
		//查询报警统计
		List<PushMsgDataVo> alarmData = pushAlarmService.getAlarmData();
		PushMessageParamsVo andParams = new PushMessageParamsVo();
		andParams.setAppkey(MsgKeys.ANDROID_KEY.getFlag());
		andParams.setAppMasterSecret(MsgKeys.ANDROID_SECRET.getFlag());
		PushMessageParamsVo iosParams = new PushMessageParamsVo();
		iosParams.setAppkey(MsgKeys.IOS_KEY.getFlag());
		iosParams.setAppMasterSecret(MsgKeys.IOS_SECRET.getFlag());
		
		UmengNotification andNotification = (UmengNotification)SpringContextUtil.getBean(AndListNotification.class);
		UmengNotification iosNotification = (UmengNotification)SpringContextUtil.getBean(IOSListNotification.class);
		
		//推送报警
		for(PushMsgDataVo vo : alarmData){
			String content = "故障总数为"+ vo.getTotalFaults()+"次; 行为总数为"+vo.getTotalBhvs() + "次; 总里程为"+vo.getTotalMile()
					+"; 总油耗为"+vo.getTotalOilEnergy()+"; 总电耗为"+vo.getTotalElecEnergy()+"; 总气耗为"+vo.getTotalGasEnergy();
			//查询角色对应用户，再根据用户查询token
			//token列推 500条推送一次
			//安卓推送
			vo.setPhoneType("OLOG0011");
			List<SysAppCanPhoneRelEntity> andUserTokens = pushAlarmService.getUserTokens(vo);
			pushMsg(content, andParams, andNotification, andUserTokens);
			//IOS推送
			vo.setPhoneType("OLOG0013");
			List<SysAppCanPhoneRelEntity> iosUserTokens = pushAlarmService.getUserTokens(vo);
			pushMsg(content, iosParams, iosNotification, iosUserTokens);
			
		}
	}
	
	
	
	
	/**
	 * 封装推送信息
	 * @param content
	 * @param params
	 * @param notification
	 * @param tokenStr
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	public void pushMsg(String content, PushMessageParamsVo params, 
			UmengNotification notification, List<SysAppCanPhoneRelEntity> userTokens)  throws Exception{
		int count = 0;
		String tokenStr = "";
		if(PubUtil.isNotEmpty(userTokens)){
			for(int i=0;i<userTokens.size();i++){
				count++;
				tokenStr += "," + userTokens.get(i).getTokenId();
				params.setUserId(userTokens.get(i).getUserId());
				params.setMsgDate(dateFormat.format(new Date()));
//				params.setDelayTime(dateFormat.format(new Date()));
				if(count%500==0){
					initSendMsg(content, params, notification, tokenStr);
					count = 0;
					tokenStr = "";
				}
			}
			initSendMsg(content, params, notification, tokenStr);
		}
	}

	private void initSendMsg(String content, PushMessageParamsVo params,
			UmengNotification notification, String tokenStr) throws Exception {
		log.error("=========================tokenStr: "+tokenStr);
		params.setContent(content);
		params.setTokenStr(tokenStr.substring(1));
		params.setTitle("数据报告");
		params.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		sendMsg(params, notification);
	}
	
	
	
	
	/**
	 * 列推
	 * @param params
	 * @param notification
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	private void sendMsg(PushMessageParamsVo params, UmengNotification notification) throws Exception {
		String msgId = UtilString.getUUID();
		params.setMsgId(msgId);
		String sendMsg = notification.sendMsg(params);
		log.error("=========================sendMsg: "+sendMsg);
	}
	
	
	
	
	
}
